package com.czxy.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 初始化Spring容器
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(SpringMVCConfig.class);
        applicationContext.register(SpringConfig.class);
        applicationContext.register(MybatisConfig.class);
        applicationContext.setServletContext(servletContext);

        // 设置核心控制器
        ServletRegistration.Dynamic springMvc = servletContext.addServlet("springMvc", new DispatcherServlet(applicationContext));
        springMvc.addMapping("*.action");
        springMvc.setLoadOnStartup(2);

        //3 post乱码配置
        FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8"));
        encodingFilter.addMappingForUrlPatterns(null, true, "/*");


    }
}
